package com.kungfuhacking.wristbandpro.my.activity;

import android.app.DatePickerDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.DatePicker;
import android.widget.RelativeLayout;

import com.facebook.drawee.view.SimpleDraweeView;
import com.kungfuhacking.wristbandpro.R;
import com.kungfuhacking.wristbandpro.base.presenter.PhotoPresenter;
import com.kungfuhacking.wristbandpro.base.view.BaseActivity;
import com.kungfuhacking.wristbandpro.custom.EnterItemView;
import com.kungfuhacking.wristbandpro.custom.TitleBarView;
import com.kungfuhacking.wristbandpro.my.presenter.ISettingPresenter;
import com.kungfuhacking.wristbandpro.my.view.ISettingView;
import com.kungfuhacking.wristbandpro.utils.AbDateUtil;
import com.kungfuhacking.wristbandpro.utils.Constant;
import com.kungfuhacking.wristbandpro.widget.ActionSheetDialog;
import com.kungfuhacking.wristbandpro.widget.MyDatePicker;

import java.util.Calendar;
import java.util.Date;


/**
 * 个人信息
 * Created by Aaron on 17/5/12.
 */

public class SettingActivity extends BaseActivity implements View.OnClickListener, ISettingView {

    private static final int MODIFY_MY_NAME_CODE = 0x0010;//修改我的姓名Code
    private static final int CHANGE_SEX_CODE = 0x0011;//修改性别code
//    private static final int MODIFY_MY_PHONE_CODE = 0x0012;//

    private SimpleDraweeView ivphoto;
    private ISettingPresenter iSettingPresenter = new ISettingPresenter(this);
    private EnterItemView username;
    private EnterItemView sex;
    private EnterItemView eivbirthday;
    private EnterItemView tel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_setting);

        initWidget();
        iSettingPresenter.setMyInfo();

    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    private void initWidget() {

        EnterItemView modifycode = (EnterItemView) findViewById(R.id.modifycode);
        tel = (EnterItemView) findViewById(R.id.tel);
        eivbirthday = (EnterItemView) findViewById(R.id.birthday);
        sex = (EnterItemView) findViewById(R.id.sex);
        username = (EnterItemView) findViewById(R.id.username);
        RelativeLayout rlheadpic = (RelativeLayout) findViewById(R.id.rl_head_pic);
        ivphoto = (SimpleDraweeView) findViewById(R.id.iv_photo);
        TitleBarView tbv = (TitleBarView) findViewById(R.id.tbv);

        tbv.setOnLeftClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        rlheadpic.setOnClickListener(this);
        username.setOnClickListener(this);
        sex.setOnClickListener(this);
        eivbirthday.setOnClickListener(this);
        modifycode.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.rl_head_pic://
                showChoose();
                break;

            case R.id.username://修改姓名
                nextActivityWithCode(ModifyNameActivity.class, MODIFY_MY_NAME_CODE);
                break;

            case R.id.sex://修改性别
                nextActivityWithCode(ModifySexActivity.class, CHANGE_SEX_CODE);
                break;

            case R.id.birthday://修改生日
                modifyBirthday();
                break;

            case R.id.modifycode://修改密码
                nextActivity(ModifyPwdActivity.class);
                break;

        }
    }

    /**
     * 修改生日
     */
    private void modifyBirthday() {
        String birthday = sp.getString(Constant.BIRTHDAY, "");
        Date date = null;

        if (!TextUtils.isEmpty(birthday)) {
            date = AbDateUtil.getDateByFormat(birthday, AbDateUtil.DATEFORMATYMD);
        }

        Calendar c = Calendar.getInstance();
        if (date != null) {
            c.setTime(date);
        }

        new MyDatePicker(this, new DatePickerDialog.OnDateSetListener() {

            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear,
                                  int dayOfMonth) {

                String month = (monthOfYear + 1) < 10 ? "0" + (monthOfYear + 1) : (monthOfYear + 1) + "";
                String day = dayOfMonth < 10 ? "0" + dayOfMonth : dayOfMonth + "";

                String curBirthday = year + "-" + month + "-" + day;

//                eivbirthday.setRightTxt(year + "-" + month + "-" + day);

                iSettingPresenter.upDateUserInfo(sp.getString(Constant.NICKNAME, ""), curBirthday,
                        sp.getString(Constant.SEX, ""), sp.getString(Constant.USER_IMG, ""));

            }

        }, c.get(Calendar.YEAR), c.get(Calendar.MONTH),
                c.get(Calendar.DAY_OF_MONTH)).show();
    }

    /**
     * 弹出选择照片的对话框
     */
    @Deprecated
    protected void showChoose() {

        new ActionSheetDialog(this)
                .builder()
                .setCancelable(true)
                .setCanceledOnTouchOutside(true)
                .addSheetItem("拍照", ActionSheetDialog.SheetItemColor.Blue,
                        new ActionSheetDialog.OnSheetItemClickListener() {
                            @Override
                            public void onClick(int which) {
                                // 拍照
                                iSettingPresenter.getTakePhoto(PhotoPresenter.TAKE_PHOTO_CODE, SettingActivity.this);

                            }

                        })
                .addSheetItem("去相册选择", ActionSheetDialog.SheetItemColor.Blue,
                        new ActionSheetDialog.OnSheetItemClickListener() {
                            @Override
                            public void onClick(int which) {
                                // 从相册中选择
                                iSettingPresenter.getFromPhotoAlbum(PhotoPresenter.PHOTO_ALBUM_CODE, SettingActivity.this);
                            }
                        }).show();

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        switch (requestCode) {

            case PhotoPresenter.TAKE_PHOTO_CODE://拍照返回的code, 裁剪

                if (iSettingPresenter.getCurrentFile() != null && iSettingPresenter.getCurrentFile().exists())
                    iSettingPresenter.startPhotoZoom(Uri.fromFile(iSettingPresenter.getCurrentFile()));//裁剪

                break;

            case PhotoPresenter.PHOTO_RESOULT://裁剪后的图片

                if (data == null)
                    return;

                iSettingPresenter.getToken(data);

                break;

            case PhotoPresenter.PHOTO_ALBUM_CODE://相册选择返回的code
                if (data == null)
                    return;
                iSettingPresenter.startPhotoZoom(iSettingPresenter.checkSelectPhoto(data, this));//开始裁剪

                break;

            case MODIFY_MY_NAME_CODE://修改昵称

                if (data == null)
                    return;

                iSettingPresenter.upDateUserInfo(data.getStringExtra("nickname"), sp.getString(Constant.BIRTHDAY, "")
                        , sp.getString(Constant.SEX, ""), sp.getString(Constant.USER_IMG, ""));

                break;

            case CHANGE_SEX_CODE://修改性别

                if (data == null)
                    return;

                iSettingPresenter.upDateUserInfo(sp.getString(Constant.NICKNAME, ""), sp.getString(Constant.BIRTHDAY, "")
                        , data.getStringExtra("modifySex"), sp.getString(Constant.USER_IMG, ""));

                break;

        }

    }

    @Override
    public void setImageUrl(String url) {
        ivphoto.setImageURI(Uri.parse(TextUtils.isEmpty(url)? "" : url));
    }

    @Override
    public void setUsername(String name) {
        username.setRightTxt(name);
    }

    @Override
    public void setSex(String s) {
        sex.setRightTxt(s);
    }

    @Override
    public void setBirthday(String birth) {
        eivbirthday.setRightTxt(birth);
    }

    @Override
    public void setTel(String phoneNum) {
        tel.setRightTxt(phoneNum);
    }

    @Override
    public String getUsername() {
        return username.getRightTxt();
    }

    @Override
    public String getMySex() {
        return sex.getRightTxt();
    }

    @Override
    public String getBirthaday() {
        return eivbirthday.getRightTxt();
    }

//    @Override
//    public String getTel() {
//        return null;
//    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        iSettingPresenter.clearRequest();
    }
}
